package net.wanho.personalOffice.service.impl;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.service.impl.BaseServiceImpl;
import net.wanho.personalOffice.dao.CommentDAO;
import net.wanho.personalOffice.dao.DailyRecordDAO;
import net.wanho.personalOffice.po.Comment;
import net.wanho.personalOffice.po.DailyRecord;
import net.wanho.personalOffice.service.DailyRecordService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DailyRecordServiceImpl extends BaseServiceImpl<DailyRecordDAO,DailyRecord> implements DailyRecordService {

    private CommentDAO commentDAO;

    @Override
    public List<DailyRecord> findAll() throws ServiceException {
        List<DailyRecord> dailyRecordList = super.baseDAO.selectAll();
        //遍历，查询，插入进去
        for (DailyRecord dailyRecord : dailyRecordList) {
            Long count = commentDAO.selectCountByRecord(dailyRecord.getRecordId());
            dailyRecord.setCommentCount(count);
        }
        return dailyRecordList;
    }

    @Override
    public boolean save(DailyRecord entity) throws ServiceException {
        return super.baseDAO.insert(entity);
    }

    @Override
    public DailyRecord findById(String id) throws ServiceException {
        DailyRecord dailyRecord = super.baseDAO.findById(id);
        Map<String, String> condMap = new LinkedHashMap<>();
        condMap.put("recordId",id);
        List<Comment> comments = commentDAO.selectByCond(condMap);
        dailyRecord.setCommentList(comments);
        return dailyRecord;
    }

    @Override
    public boolean saveComment(Comment comment) throws ServiceException {
        return commentDAO.insert(comment);
    }
}
